<?php
namespace addons\hellozjx_mapwxapp\inc\page;

trait Setting
{
	private static $_W;
	private static $_GPC;

	// 数据表
	protected $table_banner = 'hellozjx_mapwxapp_banner';
	protected $table_room = 'hellozjx_mapwxapp_classroom';
	protected $table_room_teacher = 'hellozjx_mapwxapp_teachrelation';
	protected $table_teacher = 'hellozjx_mapwxapp_teacher';
	protected $table_video = 'hellozjx_mapwxapp_video';
	protected $table_subject = 'hellozjx_mapwxapp_subject';
	protected $table_comment = 'hellozjx_mapwxapp_comment';
	protected $table_collection = 'hellozjx_mapwxapp_collection';
	protected $table_user = 'hellozjx_mapwxapp_user';
	protected $table_order = 'hellozjx_mapwxapp_order';

	public function __construct()
	{
		global $_W, $_GPC;
		self::$_W = $_W;
		self::$_GPC = $_GPC;
	}

	/**
	 * 首页 Banner 轮播图
	 * @return [type] [description]
	 */
	public function doPageBanner()
	{
		$result = pdo_getall($this->table_banner, array('uniacid' => self::$_W['uniacid']));
		return $this->result(0, '', empty($result) ? [] : $result);
	}

	/**
	 * 获取所有课堂
	 * @return [type] [description]
	 */
	public function doPageRoom()
	{
		// alive
		$result = pdo_getall($this->table_room, array('uniacid' => self::$_W['uniacid']));
		foreach ($result as $key => $val) {
			$result[$key]['alive'] = false;
			if ($key < 1) {
				$result[$key]['alive'] = true;
			}
		}
		return $this->result(0, '', empty($result) ? [] : $result);
	}

	/**
	 * 获取对应课堂的所有视频
	 * @return [type] [description]
	 */
	public function doPageVideo()
	{
		$room_id = self::$_GPC['room_id'];
		$uniacid = self::$_W['uniacid'];
		$sql = "select b.img_url, b.name, b.id, b.price, b.is_charge, a.name as teacher_name, a.city from " . tablename($this->table_video) . " as b left join " . tablename($this->table_teacher) . " as a ON b.teacher_id = a.id where b.room_id = {$room_id} and b.uniacid = {$uniacid}";
		$result = pdo_fetchall($sql);
		return $this->result(0, '', $result);
	}

	/**
	 * 获取老师分布城市 
	 * @return [type] [description]
	 */
	public function doPageCityList()
	{
		$uniacid = self::$_W['uniacid'];
		$sql = "select id, city from " . tablename($this->table_teacher) . " group by city order by id asc";
		$result = pdo_fetchall($sql);
		$config = $this->module['config'];
		$area = array(array('id'=>0,'name'=>'全部'));
        if (!empty($result)) {
            foreach ($result as $key => $value) {
                $area[] = array('id'=>$value['id'],'name'=>$value['city']);
            }
        }
		$config['icon']=tomedia($config['icon']);
        $data = array('area' => $area ?: array(),'config'=>$config);
		return message(error(0, $data), '', 'ajax');
	}

	/**
	 * [微信小程序支付]
	 * @return [type] [description]
	 */
	public function doPagePay()
	{
		//获取订单号，保证在业务模块中唯一即可
        $orderid = date('YmdHis') . mt_rand(1000, 9999);// 订单编号
        //构造支付参数
        $order = array(
            'tid' => $orderid,
            'user' => self::$_W['openid'], //用户OPENID
            'fee' => 0.01, //floatval(self::$_GPC['fee']), //金额
            'title' => '小程序支付示例',
        );
    
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, '支付失败，请重试');
        }
        $user = pdo_get($this->table_user, array('openid' => self::$_W['openid']));
        // 同时生成自定义订单
        $orderData = array(
        	'payment_no' => $orderid,
        	'goods_no' => $orderid,
        	'openid' => self::$_W['openid'],
        	'vid' => self::$_GPC['vid'],
        	'pay_time' => date('Y-m-d H:i:s'),
        	'orer_time' => date('Y-m-d H:i:s'),
        	'pay_price' => self::$_GPC['fee'],
        	'status' => 1,
        	'uid' => $user['id']
        );
       	pdo_insert($this->table_order, $orderData);
        return $this->result(0, '', $pay_params);
	}
	
}